package com.xiaotao.learn.thread.concurrent.cas;


import com.xiaotao.learn.thread.concurrent.cas.simple.NumberHandler;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class AtomicTest {
    public static void main(String[] args) throws BrokenBarrierException, InterruptedException {
        NumberHandler a = new AtomicHandler();
        CyclicBarrier barrier = new CyclicBarrier(5);
        for (int i = 0; i < 4; i++) {
            new Thread(() -> {
                for (int j = 0; j < 100000; j++) {
                    a.increase();
                }
                try {
                    barrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    e.printStackTrace();
                }
            }).start();
        }
        barrier.await();
        System.out.println(a.getValue());
    }
}
